package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.constants.Constant;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
//这个Controller事件上应该是写在ActivityController中的，但是由于写太长会不好维护，所以就分开了
public class ActivityRemarkController {

    @Autowired
    private ActivityRemarkService activityRemarkService;

    @RequestMapping("/workbench/activity/detail/saveRemark.do")
    @ResponseBody
    public Object saveRemark(ActivityRemark remark, HttpSession session) {
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        //封装数据
        remark.setId(UUIDUtils.getUUID());
        remark.setCreateTime(DateUtils.formateDataTime(new Date()));
        remark.setCreateBy(user.getId());
        remark.setEditFlag(Constant.REMARK_EDIT_NO_EDITED);

        try {
            //调用Service
            int result = activityRemarkService.saveActivityRemark(remark);

            if (result > 0) {
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
                returnObject.setRetData(remark);
            } else {
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙请稍后再试！");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/activity/detail/removeActivityRemark.do")
    @ResponseBody
    public Object removeActivityRemark(String id) {
        ReturnObject returnObject = new ReturnObject();
        //调用Service
        try {
            int result = activityRemarkService.deleteActivityRemark(id);
            if (result > 0) {
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
            } else {
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后再试...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后再试...");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/activity/detail/saveActivityRemark.do")
    @ResponseBody
    public Object saveActivityRemark(ActivityRemark remark, HttpSession session) {
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        //封装参数
        remark.setEditBy(user.getId());//修改者
        remark.setEditFlag(Constant.REMARK_EDIT_YES_EDITED);//修改标
        remark.setEditTime(DateUtils.formateDataTime(new Date()));//修改时间

        ReturnObject returnObject = new ReturnObject();
        //调用service
        try {
            int result = activityRemarkService.updateActivityRemark(remark);
            if (result > 0) {
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
                returnObject.setRetData(remark);
            } else {
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后再试...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后再试...");
        }
        return returnObject;
    }
}
